/**
 * Created by x298017064010 on 17/3/23.
 *
 * 更改应用状态的唯一方法(同步事务), 调用: this.$store.commit('increment', otherProps)
 */
const storage = weex.requireModule('storage');

// root
export function increment (state, payload) {
    state.count += payload.amount;
}

// todo
export function ADD_TODO_MSG (state, payload) {
    // 更新状态
    state.todo.todoList.push(payload.msg);
    // 存数据库
    storage.setItem('todoList', JSON.stringify(state.todo.todoList), event => {
        console.log('add', event.result)
    })
}
export function DELETE_TODO_MSG (state, payload) {
    // 更新状态
    state.todo.todoList.splice(payload.index, 1);
    // 存数据库
    storage.setItem('todoList', JSON.stringify(state.todo.todoList), event => {
        console.log('delete', event.result)
    })
}
export function QUERY_TODO_MSG (state, payload) {
    // 读数据库
    storage.getItem('todoList', event => {
        console.log('query', event.result)
        JSON.parse(event.data).forEach(function(value, index, array) {
            state.todo.todoList.push(value);
        });
    });
}

